// 加载模块
const mongoose = require("mongoose");
// 用es6中的Promise来取代 mongoose里集成的已经过时的
// promise
mongoose.Promise = Promise;
// 连接数据库
mongoose.connect("mongodb://localhost/database_ssy");
const db = mongoose.connection;
// 是否打开数据库成功
db.on("open", () => {
  console.log("打开数据库成功！");
});

// users集合
const User = mongoose.model("user", {
  name: String,
  password: String,
  isMale: Boolean,
  email: String,
  course: String,
  time: Number,
  ip: String,
  photo: String,
  info: String
});

// questions集合
const Question = mongoose.model("question", {
  // 文本内容 问题内容
  text: String,
  // 提问者
  createUser: {
    type: "ObjectId",
    ref: "user"
  },
  createTime: Number,
  ip: String,
  answers: [{
    type: "ObjectId",
    ref: "answer"
  }]
});

// answers集合
const Answer = mongoose.model("answer", {
  // 文本内容 问题内容
  text: String,
  // 回答的问题
  question: String,
  // 回答者
  createUser: {
    type: "ObjectId",
    ref: "user"
  },
  createTime: Number,
  ip: String
});


module.exports.User = User;
module.exports.Question = Question;
module.exports.Answer = Answer;
